window.onload=function(){
    //获取用户信息
    axios.defaults.withCredentials = true;
    
    
     axios.get('https://elm.cangdu.org/v1/user').then(res => {
        console.log(res);
      
       
        //获取收货地址列表
        $().ajax({
            url:`https://elm.cangdu.org/v1/users/${res.data.user_id}/addresses`,
            method:'get',
            async:true,
            success:(data)=>{
                console.log(data);
               
                for (let i = 0; i < data.length; i++) {
                    $('.kh_addressBoxs').html=' '
                    let addressbox = $().create('div').addClass('kh_addressbox')
                    .html(`<header>${data[i].address}<p>${data[i].phone}</p></header>
                        <header class='kh_deladdress'>X</header>
                    `);
                    $('.kh_addressBoxs').append(addressbox)
                    
                }
                let kh_deladdress=document.getElementsByClassName("kh_deladdress")
                for (let i = 0; i< data.length; i++) {
                    kh_deladdress[i].addEventListener('click',()=>{
                       
                        $().ajax({
                            url:`https://elm.cangdu.org/v1/users/${res.data.user_id}/addresses/${data[i].id}`,
                            method:'DELETE',
                            async:true,
                            success:(res)=>{
                                console.log(res);
                                Toast('success',res.success)
                                window.location.href='/pages/address/address.html'  
                            }
                        })
                    })
                   
                }
            }
        })
    })
    $('#kh_nowdel').click(function(){
        $('#kh_dels').html(`<span id='kh_donedel'>完成</span>`)
        let kh_deladdress=document.getElementsByClassName("kh_deladdress")
        for (let i = 0; i < kh_deladdress.length; i++) {
            kh_deladdress[i].style.display='block'
           
        } 
    $('#kh_donedel').click(function(){
        $('#kh_dels').html(`<span id='kh_nowdel'>编辑</span>`)
        for (let i = 0; i < kh_deladdress.length; i++) {
            kh_deladdress[i].style.display='none'
           
        } 
    })    

        
    })
   


    //新增地址
    $('.kh_creatBox').click(function(){
        window.location.href=`/pages/address/newaddress.html`
    })
    //获取收货地址列表
}